﻿import { onMounted, onUnmounted, ref } from 'vue'
export function useWidth() {
    const width=ref(0)
    const changeSize = () => {
    console.log('尺寸变了')
    width.value=window.innerWidth
    }
    onMounted( () => {
    changeSize()   //组件挂载后  先获取一次
    window.addEventListener('resize',changeSize)
    })


    //卸载页面，移除监听器
    onUnmounted( () => {
    window.removeEventListener('resize',changeSize)
    })
    return {
        width,
        changeSize
    }
}